package com.eclab.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/5/12 12:43
 */

@Slf4j
@Component
public class RedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    public void saveAccount(){
        redisTemplate.boundValueOps("accountName").set("ziAng");
    }

    public void saveProvince(){
        redisTemplate.boundHashOps("province").put("key1","beijing");
        redisTemplate.boundHashOps("province").put("key2","shanghai");
        redisTemplate.boundHashOps("province").put("key3","nanjing");
        redisTemplate.boundHashOps("province").put("key4","tianjin");
    }

    public void getAccount(){
        String name = (String)redisTemplate.boundValueOps("accountName").get();
        log.info("accountName",name);
        System.out.println("---------------------");
        System.out.println(name);
    }

    public void setValue(){
        redisTemplate.boundSetOps("person").add("harden");
        redisTemplate.boundSetOps("person").add("james");
        redisTemplate.boundSetOps("person").add("jordan");
    }

    public void getValue(){
        Set set = redisTemplate.boundSetOps("person").members();
        for (Object person:set){
            System.out.println(person);
        }
    }

    //删除set中的一个元素
    public void removeValue(){
        redisTemplate.boundSetOps("person").remove("james");
        Set set = redisTemplate.boundSetOps("person").members();
        for (Object person:set){
            System.out.println(person);
        }
    }

    //删除整个set
    public void deleteValue(){
        redisTemplate.delete("person");
    }

    public void listValue(){
        redisTemplate.boundListOps("user").rightPush("harden");
        redisTemplate.boundListOps("user").rightPush("park");
        redisTemplate.boundListOps("user").rightPush("duke");

        //redis查询所有，通过设置end无限大
        List lists = redisTemplate.boundListOps("user").range(0,2);
        for(Object list:lists){
            System.out.println(list);
        }
    }

    public void listLeftValue(){
        redisTemplate.boundListOps("user1").leftPush("刘备");
        redisTemplate.boundListOps("user1").leftPush("关羽");
        redisTemplate.boundListOps("user1").leftPush("张飞");
        redisTemplate.boundListOps("user1").leftPush("关羽");
        redisTemplate.boundListOps("user1").leftPush("关羽");
        List lists = redisTemplate.boundListOps("user1").range(0,10);
        for (Object list:lists){
            System.out.println(list);
        }
    }


    //移除list中的指定元素，第一个参数表示移除次数，第二个是移除的对象
    public void removeList(){
        redisTemplate.boundListOps("user1").remove(1,"关羽");
    }

    public void addHash(){
        redisTemplate.boundHashOps("keyPerson").put("person1","tangsen");
        redisTemplate.boundHashOps("keyPerson").put("person2","wukong");
        redisTemplate.boundHashOps("keyPerson").put("person3","bajie");
        redisTemplate.boundHashOps("keyPerson").put("person4","shaseng");
    }

    public void getHash(){
        Set sets = redisTemplate.boundHashOps("keyPerson").keys();
        for (Object set:sets){
            System.out.println(set);
        }
    }

    public String getHashKey(){
        String str = (String) redisTemplate.boundHashOps("keyPerson").get("person1");
        return str;
    }

    public void deleteHashKey(){
        redisTemplate.boundHashOps("keyPerson").delete("person3");
        this.getHash();
    }
}
